package cn.gok.oa.information.service.impl;

import cn.gok.oa.information.dto.InformationDto;
import cn.gok.oa.information.entity.TbInformation;
import cn.gok.oa.information.mapper.TbCommentMapper;
import cn.gok.oa.information.mapper.TbInformationMapper;
import cn.gok.oa.information.service.ITbInformationService;
import cn.gok.oa.information.vo.CommentVo;
import cn.gok.oa.information.vo.InformationVo;
import cn.gok.oa.log.LogAnnotation;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gok
 * @since 2022-08-04
 */
@Service
public class TbInformationServiceImpl extends ServiceImpl<TbInformationMapper, TbInformation> implements ITbInformationService {
    @Autowired
    TbInformationMapper tbInformationMapper;
    @Autowired
    TbCommentMapper tbCommentMapper;

    @Override
    public List<TbInformation> info(InformationDto informationDto) {
        Page<TbInformation> page = new Page<>(informationDto.getLimit(),informationDto.getPage());
        //组装查询条件
        QueryWrapper<TbInformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(informationDto.getSearch()),"title",informationDto.getSearch());
        tbInformationMapper.selectPage(page,queryWrapper);
        //调用mybatis plus 查询分页
        return page.getRecords();
    }

    @Override
    public JSONObject getOneInfo(Integer id) {
        TbInformation tbInformation=  tbInformationMapper.selectById(id);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Information",tbInformation);
        Map<String, Object> map = new HashMap<>();
        map.put("information_id", tbInformation.getId());
        jsonObject.put("Comment",tbCommentMapper.selectByMap(map));
        return jsonObject;
    }

    @Override
    public IPage<InformationVo> selectCommentList(Page<InformationVo> page, String search) {

        return tbInformationMapper.selectInformationList(page,search);
    }
}
